<?php

  //////////////////////////////////////////////////////////////////
  // OrbitFAQ                                                     //
  // ---------                                                    //
  //                                                              //
  // Orbit FAQ was solely written and developed by Orbit Services //
  // http://www.orbitservices.net                                 //
  //                                                              //
  // Access the Forum here:                                       //
  // http://forums.orbitservices.net/index.php?c=4                //
  //                                                              //
  // OrbitFAQ utilises the following opensource projects/classes; //
  //  + Fckeditor - http://www.fckeditor.net                      //
  //  + Smarty Template Engine - http://smarty.php.net            //
  //  + Swift Email Class - http://www.swiftmailer.org/           //
  //  + OWASP PHP Filter Project - http://www.owasp.org           //
  //  + MySQL Search Class by Stephen Bartholomew                 //
  //                                                              //
  //////////////////////////////////////////////////////////////////

  if (file_exists('setup')) {
    ?><font face=verdana>
    <h1>Setup File Exists</h1>
    if you have setup your OrbitFAQ Installation please delete this file.<br />
    <a href="setup/">Start Install Here</a>
    <?php
    exit;
  }

  require_once('incs/config.inc.php');
  require_once('incs/db.inc.php');
  require_once('incs/classes.inc.php');

  if($GatherUserStats == '1')
    {
      require('incs/stats.inc.php');
    }

  //checkIfLoggedIn();

  $smarty = new Smarty;

  require_once('incs/common.inc.php');

  $SmartyCompileDir = "skins/$defaultSkin/skins_c";
  $smarty->compile_dir = $SmartyCompileDir;

  $smarty->compile_check = false;
  $smarty->debugging = false;

  if($_POST['SetSkin'] != '')
    {
      $skin = $_POST['SetSkin'];
      $message = changeSkin($skin);

      header("Location: index.php?faq=$faq&ca=$ca");
      exit;
    }

  $smarty->assign('SkinListing',$SkinListing);


  // Lets get Top5 Lists
  if($MySQL_Version == '5')
  {
    require('incs/top5.inc.php');
  }

  // Lets work out where we are
  $wai = "<div>";

    if($defaultFAQ == '')
      {
        $wai_home = $OrbitFAQTitle;
        $wai .= "<a href='index.php' title='Home'><b>$wai_home</b></a>";

      }

    if($_GET['faq'] != '')
      {
        $faq = $_GET['faq'];

        $query_faq = "SELECT title from orbitfaq WHERE `id` = '$faq'";
        $result_faq = $faqsql_query ($query_faq)OR DIE( "$sql_query_error $query_faq");

        while ($row_faq = $faqsql_fetch_array ($result_faq)){
          $wai_faq = $row_faq[0];
        }

        $faq_title = $wai_faq;
        $smarty->assign("faq_title","$faq_title");

        if($defaultFAQ != '')
          {
            $wai .= "<a href='index.php?faq=$faq' title='FAQ'><B>$wai_faq</b></a>";
          }
        else
          {
            $wai .= " &raquo; <a href='index.php?faq=$faq' title='FAQ'>$wai_faq</a>";
          }


      }

    if($_GET['ca'] != '')
      {
        $ca = $_GET['ca'];

        $query_ca = "SELECT title from orbitfaq_categories WHERE `id` = '$ca'";
        $result_ca = $faqsql_query ($query_ca)OR DIE( "$sql_query_error $query_ca");

        while ($row_ca = $faqsql_fetch_array ($result_ca)){
          $wai_ca = $row_ca[0];
        }

        $wai .= " &raquo; <a href='index.php?faq=$faq&amp;ca=$ca' title='Category'>$wai_ca</a>";

      }

    $wai .= "</div>";

    $smarty->assign("WhereAmI","$wai");
    $smarty->assign("CategoryShowAnsLevel","$showAlevel");
    $smarty->assign("OrbitFAQVersion","$orbitfaq_version");


  // Now its time to get into it...
  if(($faq == '')AND($defaultFAQ == ''))
    {

      if($orbitfaq_accesslevel == 'none')
        {
          $public_sql = "AND `public` = '1'";
        }

      if(($orbitfaq_userid != '')AND($orbitfaq_accesslevel == 'user'))
        {
          $user_sql = "AND id = ANY (SELECT orbitfaq.id FROM orbitfaq, orbitfaq_groups, orbitfaq_users_groups, orbitfaq_groups_access WHERE (orbitfaq.public = '1') OR (orbitfaq_users_groups.u_id = $orbitfaq_userid AND orbitfaq_groups.id = orbitfaq_users_groups.g_id AND orbitfaq_groups_access.g_id = orbitfaq_groups.id AND orbitfaq.id = orbitfaq_groups_access.f_id))";
        }

      // Show FAQ Listing

      $smarty->assign("PageTitle","FAQ Listing");
      $smarty->assign("PageDesc","The following is a list of FAQ available through this site. Please make your selected");

      $query_faq = "SELECT * from orbitfaq WHERE `status` != '0' $public_sql $user_sql ORDER BY faq_order ASC";
      $result_faq = $faqsql_query ($query_faq)OR DIE( "$sql_query_error $query_faq");

      while ($row_faq = $faqsql_fetch_array ($result_faq)){
        $faq_id = $row_faq[0];
        $faq_title = $row_faq[1];
        $faq_desc = $row_faq[2];
        $faq_allow_user_posts = $row_faq[3];
        $faq_public = $row_faq[4];

          $query_cat = "SELECT * from orbitfaq_categories WHERE `f_id` = '$faq_id' AND `status` != '0' ORDER BY cat_order ASC";
          $result_cat = $faqsql_query ($query_cat)OR DIE( "$sql_query_error $query_cat");
          $count_cat = $faqsql_count_rows($result_cat);

          while ($row_cat = $faqsql_fetch_array ($result_cat)){
            $cat_id = $row_cat[0];
            $cat_f_id = $row_cat[1];
            $cat_title = $row_cat[2];
            $cat_desc = $row_cat[3];

              $query_qn = "SELECT * from orbitfaq_questions WHERE `c_id` = '$cat_id' AND `status` = '1' ORDER BY qn_order ASC";
              $result_qn = $faqsql_query ($query_qn)OR DIE( "$sql_query_error $query_qn");
              $count_qn = $faqsql_count_rows($result_qn);

               // Build Smarty Content Array
               $smarty_cat_array[] = array(
                      "cat_id" => "$cat_id",
                      "cat_f_id" => "$cat_f_id",
                      "cat_title" => "$cat_title",
                      "cat_desc" => "$cat_desc",
                      "cat_qn_count" => "$count_qn"
                );

          }

         // Build Smarty Content Array
         $smarty_faq_array[] = array(
            "faq_id" => "$faq_id",
            "faq_title" => "$faq_title",
            "faq_desc" => "$faq_desc",
            "faq_allow_user_posts" => "$faq_allow_user_posts",
            "faq_public" => "$faq_public",
            "faq_cat_count" => "$count_cat"
          );

      }

      // Send our Smarty Data
      $smarty->assign('FAQListing',$smarty_faq_array);
      $smarty->assign('CATListing',$smarty_cat_array);


      // Load our Listing Template
      $template_file = "../../skins/$defaultSkin/faq_listing.tpl";
      $smarty->display("file:$template_file");

      clearSmartyCompiled($SmartyCompileDir, $ClearSmartyCompiled);

      exit;
    }
  // if $faq is null and defaultFAQ is set
  elseif(($faq == '')AND($defaultFAQ != ''))
    {

      // Lets go to the default FAQ if its available.
      header( "Location: index.php?faq=$defaultFAQ" ) ;
      exit;

    }
  // if $faq is set
  elseif($faq != '')
    {

      // Lets prepare our checks to see if the user should be accessing this FAQ
      if(($orbitfaq_userid != '')AND($orbitfaq_accesslevel == 'user'))
        {
          $user_sql = "AND id = ANY (SELECT orbitfaq.id FROM orbitfaq, orbitfaq_groups, orbitfaq_users_groups, orbitfaq_groups_access WHERE (orbitfaq.public = '1') OR (orbitfaq_users_groups.u_id = $orbitfaq_userid AND orbitfaq_groups.id = orbitfaq_users_groups.g_id AND orbitfaq_groups_access.g_id = orbitfaq_groups.id AND orbitfaq.id = orbitfaq_groups_access.f_id))";
        }
      elseif($orbitfaq_accesslevel == 'none')
        {
          $user_sql = "AND public = '1'";
        }

      $query_faq = "SELECT * from orbitfaq WHERE `status` != '0' AND `id` = '$faq' $user_sql ORDER BY faq_order ASC";
      $result_faq = $faqsql_query ($query_faq)OR DIE( "$sql_query_error $query_faq");
      $count_faq = $faqsql_count_rows($result_faq);

      if($count_faq == 0)
        {
          $message = 'You Do Not Have Access to that FAQ';
          header( "Location: index.php?message=$message" ) ;
          exit;
        }

      $query_faq = "SELECT * from orbitfaq WHERE `status` != '0' AND `id` = '$faq' ORDER BY faq_order ASC";
      $result_faq = $faqsql_query ($query_faq)OR DIE( "$sql_query_error $query_faq");

      while ($row_faq = $faqsql_fetch_array ($result_faq)){
        $faq_id = $row_faq[0];
        $faq_title = $row_faq[1];
        $faq_desc = $row_faq[2];
        $faq_allow_user_posts = $row_faq[3];
        $faq_public = $row_faq[4];
      }

      // if $ca is null
      if($ca != '')
        {
          $cat_sql = "AND `id` = '$ca'";
        }

      if($qn != '')
        {
          $qn_sql = "AND `id` = '$qn'";
          $smarty->assign("CategoryShowAnsLevel","2");
        }

          // List Categories

          $smarty->assign("PageTitle","Category Listing");
          $smarty->assign("PageDesc","The following is a list of categories available through this FAQ.");

          $query_cat = "SELECT * from orbitfaq_categories WHERE `f_id` = '$faq' AND `status` != '0' $cat_sql ORDER BY cat_order ASC";
          $result_cat = $faqsql_query ($query_cat)OR DIE( "$sql_query_error $query_cat");
          $count_cat = $faqsql_count_rows($result_cat);

          while ($row_cat = $faqsql_fetch_array ($result_cat)){
            $cat_id = $row_cat[0];
            $cat_f_id = $row_cat[1];
            $cat_title = $row_cat[2];
            $cat_desc = $row_cat[3];

              $query_qn = "SELECT * from orbitfaq_questions WHERE `c_id` = '$cat_id' $qn_sql AND `status` = '1' ORDER BY qn_order ASC";
              $result_qn = $faqsql_query ($query_qn)OR DIE( "$sql_query_error $query_qn");
              $count_qn = $faqsql_count_rows($result_qn);

              while ($row_qn = $faqsql_fetch_array ($result_qn)){
                $qn_id = $row_qn[0];
                $qn_f_id = $row_qn[1];
                $qn_c_id = $row_qn[2];
                $qn_a_id = $row_qn[3];
                $qn_desc = $row_qn[4];

                $query_ans = "SELECT * from orbitfaq_answers WHERE `id` = '$qn_a_id'";
                $result_ans = $faqsql_query ($query_ans)OR DIE( "$sql_query_error $query_ans");
                $count_ans = $faqsql_count_rows($result_ans);

                if($count_ans == '0')
                  {
                    $ans_desc = '';
                  }

                while ($row_ans = $faqsql_fetch_array ($result_ans)){
                  $ans_id = $row_ans[0];
                  $ans_desc = $row_ans[1];
                  $ans_posted_by = $row_ans[2];

                  $ans_desc = html_entity_decode($ans_desc);

                  if(($showAlevel == '1')AND($qn == ''))
                    {
                      $ans_desc = ShortenText($ans_desc, $truncateTo);
                    }

                }

                $total_rating = 0;

                $query_rating = "SELECT rating from orbitfaq_ratings WHERE `q_id` = '$qn_id'";
                $result_rating = $faqsql_query ($query_rating)OR DIE( "$sql_query_error $query_rating");
                $count_rating = $faqsql_count_rows($result_rating);

                while ($row_rating = $faqsql_fetch_array ($result_rating)){
                  $qn_rating = $row_rating[0];

                  $total_rating = $total_rating+$qn_rating;

                }

                if($count_rating != '0')
                  {
                    $actual_rating = $total_rating/$count_rating;
                    $actual_rating = round($actual_rating,1);
                  }
                else
                  {
                    $actual_rating = 0;
                  }

                $allow_rating = '0';

                $u_id = $_SESSION['orbitfaq_userid'];
                if($u_id == '')
                  {
                    $u_id = $PHPSESSID;
                  }

                $query_u_rating = "SELECT rating from orbitfaq_ratings WHERE `q_id` = '$qn_id' AND `u_id` = '$u_id'";
                $result_u_rating = $faqsql_query ($query_u_rating)OR DIE( "$sql_query_error $query_u_rating");
                $count_u_rating = $faqsql_count_rows($result_u_rating);

                if($count_u_rating == '0')
                  {
                    $allow_rating = '1';
                  }

                if($row == '1')
                  {
                    $row = '2';
                  }
                else
                  {
                    $row = '1';
                  }


                   // Build Smarty Content Array
                   $smarty_qn_array[] = array(
                          "qn_id" => "$qn_id",
                          "qn_f_id" => "$cat_f_id",
                          "qn_c_id" => "$qn_c_id",
                          "qn_desc" => "$qn_desc",
                          "qn_ans_desc" => "$ans_desc",
                          "qn_rating" => "$actual_rating",
                          "qn_rating_votes" => "$count_rating",
                          "qn_rating_allow" => "$allow_rating",
                          "row" => "$row"
                    );

              }

           // Build Smarty Content Array
           $smarty_cat_array[] = array(
                  "cat_id" => "$cat_id",
                  "cat_f_id" => "$cat_f_id",
                  "cat_title" => "$cat_title",
                  "cat_desc" => "$cat_desc",
                  "cat_qn_count" => "$count_qn",
                  "faq_allow_user_posts" => "$faq_allow_user_posts"
            );

          }

          // Send our Smarty Data
          $smarty->assign('QNListing',$smarty_qn_array);
          $smarty->assign('CATListing',$smarty_cat_array);

          // Load our Listing Template
          $template_file = "../../skins/$defaultSkin/cat_listing.tpl";
          $smarty->display("file:$template_file");

          clearSmartyCompiled($SmartyCompileDir, $ClearSmartyCompiled);

      exit;
    }

?>
